/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.momarketing.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.momarketing.dao.TerminalDao;
import com.boxin.ims.modules.momarketing.entity.Terminal;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.modules.sys.service.SystemService;

/**
 * 手动终端Service
 * @author Jakemanse
 * @version 2013-05-28
 */
@Component
@Transactional(readOnly = true)
public class TerminalService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(TerminalService.class);
	
	@Autowired
	private TerminalDao terminalDao;
	
	@Autowired
	SystemService systemService;
	
	public Terminal get(Long id) {
		return terminalDao.findOne(id);
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-21  上午11:12:02
	 * @function <p> 根据终端用户名和密码 登录到系统  </p>
	 * @param name
	 * @param password
	 * @return
	 */
	public Terminal getTerminalByNameAndPassword(String name,String password){
		DetachedCriteria dc = terminalDao.createDetachedCriteria();
		
		if (StringUtils.isNotEmpty(name)){
			dc.add(Restrictions.eq("name", name));
		}
		
		if (StringUtils.isNotEmpty(password)){
			dc.add(Restrictions.eq("password", password));
		}
		
		List<Terminal> terminalList = terminalDao.find(dc);
		
		
		return (terminalList==null || terminalList.isEmpty()) ? null  :  terminalList.get(0);
		
	}
	
	
	public List<Terminal> find(Terminal terminal) {
		DetachedCriteria dc = terminalDao.createDetachedCriteria();
		
		if (terminal.getUser()!=null && terminal.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", terminal.getUser().getId()));
		}
		
		if (StringUtils.isNotEmpty(terminal.getName())){
			dc.add(Restrictions.eq("name",  terminal.getName() ));
		}
		
		
		
		if (StringUtils.isNotEmpty(terminal.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+terminal.getRemarks()+"%"));
		}
		
		dc.add(Restrictions.eq("delFlag", Terminal.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		
		return terminalDao.find( dc);
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-21  下午2:32:33
	 * @function <p> 判断该终端的登录名是否已占用  </p>
	 * @param name
	 * @return
	 */
	public boolean isExists(String name){
		Terminal t = new Terminal();
		t.setName(name);
		List<Terminal> tList = find(t);
		return (tList!=null  && !tList.isEmpty());
	}
	
	
	
	
	public Page<Terminal> find(Page<Terminal> page, Terminal terminal) {
		DetachedCriteria dc = terminalDao.createDetachedCriteria();
		
		if (terminal.getUser()!=null && terminal.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", terminal.getUser().getId()));
		}
		
		if (StringUtils.isNotEmpty(terminal.getName())){
			dc.add(Restrictions.like("name", "%"+terminal.getName()+"%"));
		}
		
		
		
		if (StringUtils.isNotEmpty(terminal.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+terminal.getRemarks()+"%"));
		}
		
		dc.add(Restrictions.eq("delFlag", Terminal.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		
		return terminalDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(Terminal terminal) {
		
		terminal.setPassword(systemService.entryptMD5Pssword(terminal.getPassword()));		//加密
		
		terminalDao.save(terminal);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		terminalDao.deleteById(id);
	}
	
}
